function [varargout] = parse_opt(args, varargin)
% function [varargout] = parse_opt(args, varargin)
%
% Process name-value argument pairs which can be passed in arbitrary
% order.  This function is very much like Mark A. Paskin's
% 'process_options' function (from which it borrows heavily), but aims 
% to be a bit faster and therefore is simpler and does less error checking.
%
% Example: Suppose we pass varargin = {'a', 23, 'c', 'hi'} into parse_opt:
%
%    [a,b,c] = parse_opt(varargin, 'a', 1, 'b', 2, 'c', 'test');
%
% This would result in a=23, b=2, and c='hi' as we gave values for variables 
% 'a' and 'c', but not 'b' which got the default value of 2.
% 

ni = length(varargin);

% these are just about the only things that we'll check for
if nargout*2 ~= ni
    error('parse_opt requires a name-value input pair for each output');
end
if mod(ni,2) ~= 0
     error('parse_opt requires name-value input pairs');
end

no = ni/2;
varargout = cell(1,no);
for i = 1:2:ni
    ndx = find(strcmpi(varargin{i}, args));
    if isempty(ndx)
        varargout{(i+1)/2} = varargin{i+1};
    else
        varargout{(i+1)/2} = args{ndx+1};
    end
end
